package com.example.liuxiangyun.ui.dashboard;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.liuxiangyun.AddCourseActivity;
import com.example.liuxiangyun.DatabaseOpenHelper;
import com.example.liuxiangyun.adapter.CourseAdapter;
import com.example.liuxiangyun.bean.Course;
import com.example.liuxiangyun.databinding.FragmentDashboardBinding;

import java.util.List;


//显示关键信息的界面，主页面。
public class DashboardFragment extends Fragment {

    //引用生成的绑定类，声明了一个binding变量
    private FragmentDashboardBinding binding;

    //调用视图
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        //创建了一个实例DashboardViewModel，ViewModel负责为activity或fragment提供数据
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);
        //ViewModelProvider确保了ViewModel单独的生命周期，作用是当配置更改（屏幕变换）也能保证数据不变
        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        //inflater方法将布局文件转换成视图
        View root = binding.getRoot();
        //获取布局文件的根视图


        return root;
        //返回根视图，显示界面
    }
//生命周期的回调方式
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //初始化，父类的清理工作
        binding = null;
        //视图不再被需要时，等于null，取消引用，被回收
    }


    //展示了在Fragment的onResume方法中如何初始化UI组件、加载数据、设置适配器和处理用户交互
    @Override
    //onResume生命周期的回调方式
    public void onResume() {
        //调用父类
        super.onResume();
        //创建了一个DatabaseOpenHelper实例，用于管理数据库，，执行数据库的创建，查询，更新
        DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(getActivity());
        //从数据库中获取所有的课程表
        List<Course> courseList = databaseOpenHelper.getAllCourses();
        //创建一个课程的适配器
        CourseAdapter courseAdapter = new CourseAdapter(courseList, getActivity());
        //将数据绑定到视图recyclerView上
        binding.recyclerView.setAdapter(courseAdapter);
        //为视图设置一个布局管理器，作用是垂直排列
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        binding.imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到添加课程界面
                Intent intent = new Intent(getActivity(), AddCourseActivity.class);
                startActivity(intent);
            }
        });

        boolean isAdmin = getActivity().getSharedPreferences("data", MODE_PRIVATE).getBoolean("isAdmin", false);
        if (isAdmin) {
            binding.imageButton.setVisibility(View.VISIBLE);
        } else {
            binding.imageButton.setVisibility(View.GONE);
        }





    }
}